﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEditor;
using UnityEngine;
using Util;

public class Rootobject
{
    public string expires { get; set; }
    public string description { get; set; }
    public string permission { get; set; }
    public Global global { get; set; }
    public string userId { get; set; }
    public string version { get; set; }
    public Doc[] docs { get; set; }
    public string createTime { get; set; }
    public string name { get; set; }
    public string id { get; set; }
    public string lastUpdateTime { get; set; }
    public string status { get; set; }
}

public class Global
{
    public string environment { get; set; }
    public string http { get; set; }
    public string id { get; set; }
    public string projectId { get; set; }
    public string status { get; set; }
}

public class Doc
{
    public Doc[] children { get; set; }
    public string content { get; set; }
    public string id { get; set; }
    public string name { get; set; }
    public string parentId { get; set; }
    public string projectId { get; set; }
    public int sort { get; set; }
    public string type { get; set; }
}
public class page
{
    public string requestMethod { get; set; }
    public string dataType { get; set; }
    public string contentType { get; set; }
    public Child[] requestArgs { get; set; }
    public Child[] requestHeaders { get; set; }
    public Child[] responseHeaders { get; set; }
    public Child[] responseArgs { get; set; }
    public string url { get; set; }
    public string status { get; set; }
    public string description { get; set; }
    public string example { get; set; }
}

public class Child
{
    public Child[] children { get; set; }
    public string name { get; set; }
    public string type { get; set; }
    public string require { get; set; }
    public string description { get; set; }
    public string id { get; set; }
}

public class pageData
{
    public page data;
    public Doc doc;

    public string Creation()
    {
        string v = "";
        v += "//" + doc.name + "\n";
        v = JsonJxTools.Protocols(data.url , data.requestArgs , data.responseArgs);
        return v;
    }


}

public static class JsonJxTools
{
    public static int toint(this object s)
    {
        return s.ToString().toint();
    }
    public static string tostring(this object s)
    {
        return s.ToString();
    }
    public static bool tobool(this object s)
    {
        return bool.Parse(s.ToString());
    }
    public static object toobject(this object s)
    {
        return s;
    }
    public class bag
    {
        public string name;
        public string type;
        public string ftype;
        public string zs;
        public bool must;
        public static bag New(string n , string t , string z , string m)
        {
            bag b = new bag();
            b.name = n;
            b.type = t;
            b.must = true;
            if(m.Contains("false"))
            {
                b.must = false;
            }
            else
            if(m.Contains("true"))
            {
                b.must = true;
            }

            if(b.type.Contains("number"))
            {
                b.type = "int";
                b.ftype = ".toint()";
            }
            else
            if(b.type.Contains("string"))
            {
                b.type = "string";
                b.ftype = ".tostring()";
            }
            else
            if(b.type.Contains("bool"))
            {
                b.type = "bool";
                b.ftype = ".tobool()";
            }
            else
            if(b.type.Contains("array"))
            {
                b.type = "Dictionary<string,object>[]";
                b.ftype = " as Dictionary<string,object>[]";
            }
            else
            if(b.type.Contains("object"))
            {
                b.type = "Dictionary<string,object>";
                b.ftype = " as Dictionary<string,object>";
            }
            b.zs = z;
            return b;
        }


        public static void Cre(string name , Child child , ref string com)
        {
            var n = New(child.name , child.type , child.description , child.require);
            if(!n.must)
            {
                com += "if(false)\n{\n//";
            }
            com += n.type + " " + n.name + " = " + name + "[\"" + n.name + "\"]" + n.ftype + ";//" + n.zs + "\n";

            string nexN = child.name;
            if(n.type == "Dictionary<string,object>[]")
            {
                com += "Dictionary<string,object> " + child.name + "item=" + child.name + "[0];\n";
                nexN = child.name + "item";
            }

            for(int i = 0 ; i < child.children.Length ; i++)
            {
                Cre(nexN , child.children[i] , ref com);
            }
            if(!n.must)
            {
                com += "}\n";
            }
        }
    }

    public static string Protocols(string url , Child[] gets , Child[] backs)
    {
        string v = "";
        string name = url.Replace("/" , "");
        string Get;
        {
            Get = "private void " + name + "Req()\n{\n";
            Get += "string url = \"" + url + "\";\n";
            Get += "WWWForm form = new WWWForm();\n";
            Get += reqJx(gets);
            Get += "HttpHelper.Instance.Posts(url, form, " + name + "Res);\n";
            Get += "}\n";
        }
        string func;
        {
            func = "private void " + name + "Res(Dictionary<string,object> jsonObj)\n{\n";
            func += resJx(backs);
            func += "}\n";
        }
        v = Get + func;

        return v;
    }
    public static string reqJx(Child[] com)
    {
        string func = "";
        var arr = com;

        for(int i = 0 ; i < arr.Length ; i++)
        {
            var n = bag.New(arr[i].name , arr[i].type , arr[i].description , arr[i].require);
            if(!n.must)
            {
                func += "if(false)\n{\n//";
            }
            string lin = "form.AddField(\"" + n.name + "\"," + n.name + ");//" + n.zs + "\n";
            func += lin;
            if(!n.must)
            {
                func += "}\n";
            }
        }
        return func;
    }
    public static string resJx(Child[] com)
    {
        string func = "";
        var arr = com;
        string name = "jsonObj";
        for(int i = 0 ; i < arr.Length ; i++)
        {
            var n = arr[i];
            bag.Cre(name , n , ref func);

        }
        return func;
    }
}


public class Jsonjx
{
    // public void OnGui()
    // {
    //     Inpt("" , ref txt);
    //     if(Button("创建"))
    //     {
    //         var com = Jsonjx.Creation(txt.text);
    //         string fullPath = AssetDatabase.GetAssetPath(txt);
    //         var path = Path.GetDirectoryName(fullPath);
    //         debug.log(path);
    //         FileIO.Save(path , "/Protocol.cs" , com);
    //     }
    // }
    TextAsset txt;
    public static string Creation(string txt)
    {
        Rootobject obj = SerializeJosn.Desrialize<Rootobject>(txt);

        List<Doc> doc = new List<Doc>();
        for(int i = 0 ; i < obj.docs.Length ; i++)
        {
            GetDoc(obj.docs[i] , doc);
        }

        List<pageData> pages = new List<pageData>();

        for(int i = 0 ; i < doc.Count ; i++)
        {
            var s = doc[i].content;
            page p = SerializeJosn.Desrialize<page>(s);
            if(p != null)
            {
                pageData np = new pageData();
                np.data = p;
                np.doc = doc[i];
                pages.Add(np);
            }
        }
        Dictionary<string , List<pageData>> dic = new Dictionary<string , List<pageData>>();
        for(int i = 0 ; i < pages.Count ; i++)
        {
            var c = pages[i];
            if(dic.ContainsKey(c.doc.type))
            {

            }
            else
            {
                dic.Add(c.doc.type , new List<pageData>());
            }
            dic[c.doc.type].Add(c);
        }
        string com = "namespace Https{\n";
        var e = dic.GetEnumerator();
        while(e.MoveNext())
        {
            var list = e.Current.Value;
            com += "public class " + e.Current.Key.Replace("." , "") + "{\n";
            for(int i = 0 ; i < list.Count ; i++)
            {
                com += list[i].Creation();
            }
            com += "}\n";
        }
        com += "}\n";
        debug.log(com);
        return com;

    }
    public static void GetDoc(Doc doc , List<Doc> list)
    {
        if(doc.children.Length == 0)
        {
            list.Add(doc);
        }
        for(int i = 0 ; i < doc.children.Length ; i++)
        {
            GetDoc(doc.children[i] , list);
        }


    }


}